package com.sskj.ffex.v.activity;

import android.content.Context;
import android.content.Intent;
import android.text.Editable;
import android.text.TextWatcher;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.sskj.ffex.MyAppLication;
import com.sskj.ffex.R;
import com.sskj.ffex.common.App;
import com.sskj.ffex.common.Constants;
import com.sskj.ffex.p.activity.LoginPresenter;
import com.sskj.ffex.utils.CheckUtil;
import com.sskj.ffex.utils.ClickUtil;
import com.sskj.ffex.utils.SPUtils;
import com.sskj.ffex.utils.ToastUtil;
import com.sskj.ffex.v.base.BaseActivity;

import java.util.concurrent.TimeUnit;

import butterknife.BindView;
import io.reactivex.Flowable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;
import io.reactivex.subscribers.DisposableSubscriber;

/**
 * <pre>
 *     author : wk_math
 *     e-mail : wk_math@163.com
 *     time   : 2018/03/30
 *     desc   :
 *     version: 1.0
 * </pre>
 */

public class LoginActivity extends BaseActivity<LoginPresenter> {
    @BindView(R.id.et_phone)
    EditText etPhone;
    @BindView(R.id.et_pwd)
    EditText etPwd;
    @BindView(R.id.iv_toggle_pwd)
    ImageView ivTogglePwd;  // 密码是否显示
    @BindView(R.id.bt_login)
    Button btLogin;  // 登录
    @BindView(R.id.tv_goto_register)
    TextView tvGotoRegister;
    @BindView(R.id.tv_pwd_forget)
    TextView tvPwdForget;
    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.account_login)
    TextView accountLogin;  // 账号登录
    @BindView(R.id.pwd_view)
    View pwdView;  // 账号登录下的下滑线
    @BindView(R.id.yzm_view)
    View yzmView;  // 验证码登录下的下滑线
    @BindView(R.id.login_pwd)
    TextView loginPwd;
    @BindView(R.id.pwd_show)
    RelativeLayout pwdShow;  //密码登录下显示
    @BindView(R.id.yzm_login2)
    TextView yzmLogin2; // 验证码登录
    @BindView(R.id.yzm_show)
    RelativeLayout yzmShow;  // 验证码登录下显示
    @BindView(R.id.register_input_auth_code)
    EditText registerInputAuthCode;  // 验证码输入框
    @BindView(R.id.register_get_auth_code)
    TextView registerGetAuthCode;  // 获取验证码
    private boolean isSee = false;
    private static int index;
    private int showInt = 0;  // 0 账号登录   1  验证码登录
    private DisposableSubscriber<Long> disposableSubscriber;


    @Override
    protected int getLayoutResId() {
        return R.layout.activity_login;
    }

    @Override
    public LoginPresenter getPresenter() {
        return new LoginPresenter();
    }

    @Override
    protected void onViewBinding() {
        ClickUtil.click(400, accountLogin, () -> {
            showInt = 0;
            accountLogin.setTextColor(getResources().getColor(R.color.app_blue));
            yzmLogin2.setTextColor(getResources().getColor(R.color.grayHint));
            pwdView.setVisibility(View.VISIBLE);
            yzmView.setVisibility(View.GONE);
            pwdShow.setVisibility(View.VISIBLE);
            yzmShow.setVisibility(View.GONE);

        });
        ClickUtil.click(400, yzmLogin2, () -> {
            showInt = 1;
            accountLogin.setTextColor(getResources().getColor(R.color.grayHint));
            yzmLogin2.setTextColor(getResources().getColor(R.color.app_blue));
            pwdView.setVisibility(View.GONE);
            yzmView.setVisibility(View.VISIBLE);
            pwdShow.setVisibility(View.GONE);
            yzmShow.setVisibility(View.VISIBLE);
        });

        ClickUtil.click(400, registerGetAuthCode, () -> {
            if (CheckUtil.isMobile(etPhone.getText().toString()) || CheckUtil.isEmail(etPhone.getText().toString())) {
                if (CheckUtil.isMobile(etPhone.getText().toString())) {
                    mPresenter.sendCode(etPhone.getText().toString().trim());
                } else {
                    mPresenter.sendEmail(etPhone.getText().toString().trim());
                }
            } else {
                ToastUtil.showShort(App.INSTANCE.getString(R.string.strLoginActivity201));
            }
        });


        ClickUtil.click(400, ivTogglePwd, () -> {
            if (isSee) {
                isSee = false;
                etPwd.setTransformationMethod(new PasswordTransformationMethod());
                ivTogglePwd.setImageResource(R.mipmap.unsee);
            } else {
                isSee = true;
                etPwd.setTransformationMethod(new HideReturnsTransformationMethod());
                ivTogglePwd.setImageResource(R.mipmap.see);
            }
        });
        ClickUtil.click(btLogin, () -> {
            if (CheckUtil.isMobile(etPhone.getText().toString()) || CheckUtil.isEmail(etPhone.getText().toString())) {  // 邮箱 手机号校验
                if (showInt == 0) {  // 账号登录
                    if (etPwd.getText().length() >= 6 && etPwd.getText().length() <= 20) {
                        mPresenter.login(etPhone.getText().toString(), etPwd.getText().toString());
                    } else {
                        ToastUtil.showShort(App.INSTANCE.getString(R.string.strLoginActivity200));
                    }
                } else {  // 验证码登录
                    if (registerInputAuthCode.getText().toString().length() == 6) {
                        mPresenter.yzmLogin(etPhone.getText().toString().trim(), registerInputAuthCode.getText().toString().trim());
                    } else {
                        ToastUtil.showShort(App.INSTANCE.getString(R.string.strGoogleLoginActivity202));
                    }

                }
            } else {
                ToastUtil.showShort(App.INSTANCE.getString(R.string.strLoginActivity201));
            }
        });
//        registerInputAuthCode.addTextChangedListener(new TextWatcher() {
//            @Override
//            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
//
//            }
//
//            @Override
//            public void onTextChanged(CharSequence s, int start, int before, int count) {
//
//            }
//
//            @Override
//            public void afterTextChanged(Editable s) {
//                if (s.toString().length() == 6) {
//                    btLogin.setEnabled(true);
//                } else {
//                    btLogin.setEnabled(false);
//                }
//            }
//        });
        etPhone.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (CheckUtil.isMobile(s.toString()) || CheckUtil.isEmail(s.toString())) {
                    btLogin.setEnabled(true);
                } else {
                    btLogin.setEnabled(false);
                }
            }
        });
        ClickUtil.click(tvGotoRegister, () -> startActivity(new Intent(this, RegisterActivity.class)));
        ClickUtil.click(tvPwdForget, () -> {
            startActivity(new Intent(this, PwdForgetActivity.class));
        });
        ClickUtil.click(ivBack, () -> finish());

    }

    @Override
    protected void initView() {
        index = getIntent().getIntExtra("index", 0);
    }

    /**
     * 登录
     *
     * @param context context
     */
    public static void start(Context context, int index) {
        Intent intent = new Intent(context, LoginActivity.class);
        intent.putExtra("index", index);
        context.startActivity(intent);
    }

    public static void start(Context context) {
        Intent intent = new Intent(context, LoginActivity.class);
        context.startActivity(intent);
    }


    /**
     * 登录成功
     */
    public void loginSuccess(int type) {  // 0 关闭  1 开启
//        RxBus.getDefault().send(Constants.USER_LOGIN);
        if (index != 0) {
            if (0 == type) {
                SPUtils.put(MyAppLication.getApplication(), Constants.SP_LOGIN_STATUS, true);
                finish();
            } else {
                GoogleLoginActivity.start(LoginActivity.this, 0, null);
            }
        } else {
            if (0 == type) {
                SPUtils.put(MyAppLication.getApplication(), Constants.SP_LOGIN_STATUS, true);
                startActivity(new Intent(this, MainActivity.class));
                finish();
            } else {
                GoogleLoginActivity.start(LoginActivity.this, 0, null);
                finish();
            }

        }
    }

    public void startCount() {
        registerGetAuthCode.setEnabled(false);
        disposableSubscriber = new DisposableSubscriber<Long>() {
            @Override
            public void onNext(Long aLong) {
                int time = (int) (60 - aLong);
                registerGetAuthCode.setText(String.format(App.INSTANCE.getString(R.string.strRegisterActivity210), time));
            }

            @Override
            public void onError(Throwable t) {
                System.out.println(t.toString());
            }

            @Override
            public void onComplete() {
                registerGetAuthCode.setText(App.INSTANCE.getString(R.string.strRegisterActivity211));
                registerGetAuthCode.setEnabled(true);
                if (!disposableSubscriber.isDisposed()) {
                    disposableSubscriber.dispose();
                    disposableSubscriber = null;
                }

            }
        };

        Flowable.interval(0, 1, TimeUnit.SECONDS, Schedulers.newThread())
                .take(60)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(disposableSubscriber);
    }

}
